﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
using System.Text.RegularExpressions;
using Facebook.Schema;
using Facebook.Winforms;

namespace SNAIL.Classes
{
    [Serializable()]
    public class Contact
    {
        private DateTime _LastConversationDateTime = new DateTime();

        public DateTime LastConversationDateTime
        {
            get { return _LastConversationDateTime; }
            set { _LastConversationDateTime = value; }
        }

        private DateTime _LastMessageSent = new DateTime();

        public DateTime LastMessageSent
        {
            get { return _LastMessageSent; }
            set { _LastMessageSent = value; }
        }

        private DateTime _LastMessageReceived = new DateTime();

        public DateTime LastMessageReceived
        {
            get { return _LastMessageReceived; }
            set { _LastMessageReceived = value; }
        }

        private long? _FacebookUID;

        public long? FacebookUID
        {
            get { return _FacebookUID; }
            set { _FacebookUID = value; }
        }
        private List<Contact> _ConnectedContacts = new List<Contact>();

        public List<Contact> ConnectedContacts
        {
            get { return _ConnectedContacts; }
            set { _ConnectedContacts = value; }
        }

        public static string GetContactPicturePath(Microsoft.Office.Interop.Outlook.ContactItem contact)
        {
            return GetContactPicturePath(contact, System.IO.Path.GetTempPath());
        }

        public static string GetContactPicturePath(Microsoft.Office.Interop.Outlook.ContactItem contact, string path)
        {
            string picturePath = "";

            if (contact.HasPicture)
            {
                foreach (Microsoft.Office.Interop.Outlook.Attachment att in contact.Attachments)
                {
                    if (att.DisplayName == "ContactPicture.jpg")
                    {
                        try
                        {
                            picturePath = System.IO.Path.GetDirectoryName(path) + "\\Contact_" + contact.EntryID + ".jpg";
                            if (!System.IO.File.Exists(picturePath))
                                att.SaveAsFile(picturePath);
                        }
                        catch
                        {
                            picturePath = "";
                        }
                    }
                }
            }

            return picturePath;
        }

        public Contact(MailItem MailObject)
        {
            _DisplayString = MailObject.SenderName;
            if (MailObject.SenderEmailType != "SMTP")
            {
                _ContactString = MailObject.SenderName;
            }
            else
            {
                _ContactString = MailObject.SenderEmailAddress;
            }

            if (this._ContactString == null)
            {
                throw new NullReferenceException();
            }
        }

        public Contact(ContactItem ContactObject)
        {
            _OutlookContact = ContactObject.EntryID;
            _DisplayString = ContactObject.FullName;

            if (ContactObject.Email1AddressType != "SMTP")
            {
                _ContactString = ContactObject.FullName;
            }
            else
            {
                _ContactString = ContactObject.Email1Address;
            }

            if (this._ContactString == null)
            {
                throw new NullReferenceException();
            }
        }

        public Contact(Recipient RecipientObject)
        {
            _DisplayString = RecipientObject.Name;

            if (IsEmailAddress(RecipientObject.Address))
            {
                _ContactString = RecipientObject.Address;
            }
            else
            {
                _ContactString = RecipientObject.Name;
            }

            if (this._ContactString == null)
            {
                throw new NullReferenceException();
            }
        }
        public Contact(string ContactString)
        {
            _ContactString = ContactString;
            _DisplayString = ContactString;
        }


        /// <summary>
        /// Returns if specified text is an email address
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        private static bool IsEmailAddress(string text)
        {
            return new Regex(@"^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", RegexOptions.IgnoreCase).IsMatch(text);
        }

        public Contact(string ContactString, string DisplayString)
        {
            _ContactString = ContactString;
            _DisplayString = DisplayString;
        }

        public override string ToString()
        {
            if (_UnreadMails)
            {
                return "(Unread Mail) " + _DisplayString;
            }
            else
            {
                return _DisplayString;
            }
        }

        public string ContactInformation
        {
            get
            {
                return ReceivedFromCount + " In / " + _SentToCount + " Out";
            }
        }

        private string _ExtendedInformation;

        public string ExtendedInformation
        {
            get
            {
                if (!(this._OutlookContact == null))
                {
                    return "Connected Outlook Contact";
                }
                else if (!(this._FacebookUID == null))
                {
                    return "Connected with facebook";
                }
                else if (!(_ExtendedInformation == null))
                {
                    return _ExtendedInformation;
                }
                else
                {
                    return "Unconnected Contact";
                }

            }
            set { _ExtendedInformation = value; }
        }

        public override bool Equals(object obj)
        {
            if (obj.GetType().Equals(typeof(Contact)))
            {
                Contact TempContact = (Contact)obj;
                if (TempContact.ContactString == this.ContactString)
                {
                    return true;
                }
            }

            return false;
        }

        public override int GetHashCode()
        {
            return this._ContactString.GetHashCode();
        }

        public double RatingsScore
        {
            get
            {
                //Depending on wheather the contact is connected, give the contact
                //a better rating, and don't forget to take the RatingMOdifier into
                //Account
                if (!(this._OutlookContact == null))
                {
                    return (Math.Abs(1.0 - ((Convert.ToDouble(this.SentToCount) + 0.00001) / (Convert.ToDouble(this.ReceivedFromCount) + 0.00001))) + 0.1) / (this.SentToCount + 0.00001);
                }
                else if (!(this._FacebookUID == null))
                {
                    return (Math.Abs(1.0 - ((Convert.ToDouble(this.SentToCount) + 0.00001) / (Convert.ToDouble(this.ReceivedFromCount) + 0.00001))) + 0.1) / (this.SentToCount + 0.00001);
                }
                else
                {
                    return (Math.Abs(1.0 - ((Convert.ToDouble(this.SentToCount) + 0.00001) / (Convert.ToDouble(this.ReceivedFromCount) + 0.00001))) + 0.1) / (this.SentToCount + 0.00001);
                }
            }
        }

        private double _RatingModifier;

        public double RatingModifier
        {
            get { return _RatingModifier; }
            set { _RatingModifier = value; }
        }

        private int _ReceivedFromCount;

        public int ReceivedFromCount
        {
            get { return _ReceivedFromCount; }
            set { _ReceivedFromCount = value; }
        }
        private int _SentToCount;

        public int SentToCount
        {
            get { return _SentToCount; }
            set { _SentToCount = value; }
        }

        private string _DisplayString;

        public string DisplayString
        {
            get { return _DisplayString; }
            set { _DisplayString = value; }
        }
        private string _ContactString;

        public string ContactString
        {
            get { return _ContactString; }
            set { _ContactString = value; }
        }

        [NonSerialized]
        private List<MailItem> _UnreadMailsList = new List<MailItem>();

        public List<MailItem> UnreadMailsList
        {
            get { return _UnreadMailsList; }
            set { _UnreadMailsList = value; }
        }

        [NonSerialized]
        private bool _UnreadMails;

        public bool UnreadMails
        {
            get { return _UnreadMails; }
            set { _UnreadMails = value; }
        }

        private string _OutlookContact;

        public string OutlookContact
        {
            get { return _OutlookContact; }
            set { _OutlookContact = value; }
        }

        private string _ContactImagePath;

        public string ContactImagePath
        {
            get { return _ContactImagePath; }
            set { _ContactImagePath = value; }
        }

        private ContactMessages _Messages = new ContactMessages();

        public ContactMessages Messages
        {
            get { return _Messages; }
            set { _Messages = value; }
        }

    }
}
